/**
 * 设置竖向菜单
 * */
import { setFirst, setSecond } from "./menuIndex";

const menu = "tree";

// 缓存菜单
export function setTree(data) {
  window.localStorage.setItem(menu, data);
}

// 获取缓存菜单
export function getTree() {
  return window.localStorage.getItem(menu);
}

/**
 * 设置横向菜单
 * */

const value = "menu";

// 获取
export function getHorizontalMenu() {
  return window.localStorage.getItem(value);
}

// 设置
export function setHorizontalMenu(tree) {
  return window.localStorage.setItem(value, tree);
}

/*
 * 菜单key获取
 * parent：一级菜单名称
 * url：需要获取key的二级菜单路径
 * **/
export function formatTree(parent, url) {
  let data = window.localStorage.getItem("tree");
  let tree = JSON.parse(data);
  let key = "";
  tree.forEach((item, first) => {
    if (item.name === parent) {
      item.childPermissions.forEach((menu, second) => {
        if (menu.url === url) {
          setFirst(first);
          setSecond(second);
          key = menu.id;
        }
      });
    }
  });
  return key;
}
